{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['cute', 'problems', 'quit', 'stupid', 'flea', 'park', 'has', 'garbage', 'ate', 'please', 'mr', 'steak', 'dog', 'food', 'worthless', 'my', 'love', 'not', 'help', 'so', 'buying', 'how', 'take', 'maybe', 'posting', 'licks', 'is', 'dalmation', 'stop', 'I', 'to', 'him']\n",
      "[0, 1, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]\n",
      "[-3.33220451 -3.33220451 -4.02535169 -4.02535169 -3.33220451 -4.02535169\n",
      " -3.33220451 -4.02535169 -3.33220451 -3.33220451 -3.33220451 -3.33220451\n",
      " -3.33220451 -4.02535169 -4.02535169 -2.63905733 -3.33220451 -4.02535169\n",
      " -3.33220451 -3.33220451 -4.02535169 -3.33220451 -4.02535169 -4.02535169\n",
      " -4.02535169 -3.33220451 -3.33220451 -3.33220451 -3.33220451 -3.33220451\n",
      " -3.33220451 -2.9267394 ]\n",
      "[-3.93182563 -3.93182563 -3.23867845 -2.54553127 -3.93182563 -3.23867845\n",
      " -3.93182563 -3.23867845 -3.93182563 -3.93182563 -3.93182563 -3.93182563\n",
      " -2.83321334 -3.23867845 -2.83321334 -3.93182563 -3.93182563 -3.23867845\n",
      " -3.93182563 -3.93182563 -3.23867845 -3.93182563 -3.23867845 -3.23867845\n",
      " -3.23867845 -3.93182563 -3.93182563 -3.93182563 -3.23867845 -3.93182563\n",
      " -3.23867845 -3.23867845]\n",
      "0.5\n"
     ]
    }
   ],
   "source": [
    "import numpy as np \n",
    "import math\n",
    "# 使用词集法进行贝叶斯分类\n",
    "# 构造数据集,分类是侮辱性 or 非侮辱性\n",
    "def loadDataset () :\n",
    "    postingList=[['my', 'dog', 'has', 'flea', 'problems', 'help', 'please'],\n",
    "                 ['maybe', 'not', 'take', 'him', 'to', 'dog', 'park', 'stupid'],\n",
    "                 ['my', 'dalmation', 'is', 'so', 'cute', 'I', 'love', 'him'],\n",
    "                 ['stop', 'posting', 'stupid', 'worthless', 'garbage'],\n",
    "                 ['mr', 'licks', 'ate', 'my', 'steak', 'how', 'to', 'stop', 'him'],\n",
    "                 ['quit', 'buying', 'worthless', 'dog', 'food', 'stupid']]\n",
    "    classVec = [0,1,0,1,0,1]    #1 is abusive, 0 not\n",
    "    return postingList, classVec\n",
    "\n",
    "\n",
    "# 创建一个包涵所有词汇的列表 , 为后面建立词条向量使用\n",
    "def createlist (dataset) :\n",
    "    vovabset = set ([])\n",
    "    for vec in dataset :\n",
    "        vovabset = vovabset | set (vec)\n",
    "    return list (vovabset)\n",
    "\n",
    "# 将词条转化为向量的形式\n",
    "def changeword2vec (inputdata, wordlist) :\n",
    "    returnVec = [0] * len (wordlist)\n",
    "    for word in inputdata :\n",
    "        if word in wordlist :\n",
    "            returnVec[wordlist.index(word)] = 1\n",
    "    return returnVec\n",
    "\n",
    "# 创建贝叶斯分类器 \n",
    "def trainNBO (dataset, classlebels) :\n",
    "    num_of_sample = len (dataset)\n",
    "    num_of_feature = len (dataset[0])\n",
    "    pAusuive = sum (classlebels) / num_of_sample # 侮辱性语言的概率\n",
    "    p0Num = np.ones (num_of_feature)\n",
    "    p1Num = np.ones (num_of_feature)\n",
    "    p0tot = num_of_feature\n",
    "    p1tot = num_of_feature\n",
    "    for i in range (num_of_sample) :\n",
    "        if classlebels[i] == 1 :\n",
    "            p1Num += dataset[i]\n",
    "            p1tot += sum (dataset[i])\n",
    "        else :\n",
    "            p0Num += dataset[i]\n",
    "            p0tot += sum (dataset[i])   \n",
    "    p0Vec = p0Num / p0tot\n",
    "    p1Vec = p1Num / p1tot\n",
    "    for i in range (num_of_feature) :\n",
    "        p0Vec[i] = math.log (p0Vec[i])\n",
    "        p1Vec[i] = math.log (p1Vec[i])\n",
    "    return p0Vec, p1Vec, pAusuive\n",
    "\n",
    "\n",
    "#  定义分类器 \n",
    "def classifyNB(vec2Classify, p0Vec, p1Vec, pClass1):\n",
    "    p1 = sum(vec2Classify * p1Vec) + log(pClass1)    #element-wise mult\n",
    "    p0 = sum(vec2Classify * p0Vec) + log(1.0 - pClass1)\n",
    "    if p1 > p0:\n",
    "        return 1\n",
    "    else: \n",
    "        return 0\n",
    "\n",
    "# 测试代码 \n",
    "dataset,classlebels = loadDataset ()\n",
    "wordlist = createlist (dataset)\n",
    "print (wordlist)\n",
    "print (changeword2vec (dataset[0], wordlist))\n",
    "trainmat = []\n",
    "for temp in dataset :\n",
    "    trainmat.append (changeword2vec (temp,wordlist))\n",
    "p0V, p1V, pAb = trainNBO (trainmat, classlebels)\n",
    "print (p0V)\n",
    "print (p1V)\n",
    "print (pAb)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
